<?php
namespace Auntie\Controller;
use Think\Controller;
class ApiController extends Controller {
	
    public static $session;		//SESSION
    
	public function _initialize(){
		self::$session=$_SESSION['auntie'];
	}
	//===================
    public function index(){
		$this->display();
    }
	//收衣 把选好的衣单价格放到订单里面留使用网络支付
	public function souyi(){
		if(IS_POST){
			$p=I('post.');
			$orderid=$p['orderid'];
			$order=M('bb_order')->where(array('orderid'=>$orderid))->find();
			if($order){
				//==============================
				if($order['status']==2){
					$info['status']=1;
					$info['msg']='yes';
					$this->ajaxreturn($info);
				}
				//==============================
				if($p['jinge']){
					$order['total_price']=$p['jinge'];
				}
				//================
				$order['status']=2;
				$re=M('bb_order')->save($order);
				if($re){
					$info['status']=1;
					$info['msg']='yes';
				}else{
					$info['status']=0;
					$info['msg']='no';
				}
				$this->ajaxreturn($info);
			}
		}
	}
	//收衣现金支付
	public function souyixianjin(){
		if(IS_POST){
			$p=I('post.');
			$orderid=$p['orderid'];
			$order=M('bb_order')->where(array('orderid'=>$orderid))->find();
			if($order){
				//==============================
				if($order['status']==3){
					$info['status']=1;
					$info['msg']='yes';
					$this->ajaxreturn($info);
				}
				//==============================
				if($p['jinge']){
					$order['total_price']=$p['jinge'];
				}
				//================
				$order['status']=3;
				$order['pay_price']=$order['total_price'];
				$order['is_pay']=1;
				$order['pay_type']='现金';
				//======================
				$userset=M('bb_user_set')->find();
				$consume_score=round($order['total_price']*$userset['consume_score']/100);
				$u_integral=M('client_user')->where(array('openid'=>$order['openid']))->find();
				$u_integral['integral']=($u_integral['integral']+$consume_score);
				M('client_user')->save($u_integral);
				
				$log['openid']=$order['openid'];
				$log['type']='1';   //积分日志
				$log['msg']='积分增加：下单获得'.$consume_score.'积分';
				M('log_msg')->add($log);
				//=========================
				$re=M('bb_order')->save($order);
				if($re){
					/*
					//=阿姨提成===========================================
						$pay_price=$order['total_price'];
						$aylog['auntieid']=self::$session['auntie_user']['id'];
						$aylog['orderid']=$order['orderid'];
						$aylog['orderprice']=$pay_price;
						$aylog['money']=round($pay_price*$userset['auntie_deduct']/100,2);
						$aylog['addtime']=time();
						M('auntie_log')->add($aylog);
						
						$auntie=M('bb_auntie')->where(array('id'=>self::$session['auntie_user']['id']))->find();
						$auntie['money']=($auntie['money']+$aylog['money']);
						M('bb_auntie')->save($auntie);
					//===================================================
					*/
					$info['status']=1;
					$info['msg']='yes';
					
				}else{
					$info['status']=0;
					$info['msg']='no';
				}
				$this->ajaxreturn($info);
			}
		}
	}
	//=========================
	//保存衣单的数据
	public function quedingsubmit(){
		if(IS_POST){
			$p=I('post.');
			$orderid=$p['orderid'];
			$array=array();
			foreach ($p['data']['name'] as $k => $v) {
				$array[]['name']=$v;
			}
			foreach ($array as $k => $v) {
				$array[$k]['orderid']=$p['orderid'];
				$array[$k]['num']=$p['data']['num'][$k];
				$array[$k]['price']=$p['data']['price'][$k];
				$array[$k]['toprice']=$p['data']['price'][$k]*$array[$k]['num'];
				$array[$k]['to_price']=$p['data']['to_price'][$k];
				$array[$k]['xiachi']=$p['data']['xiachi'][$k];
				$array[$k]['fujian']=$p['data']['fujian'][$k];
			}
			
			$order=M('bb_order')->where(array('orderid'=>$orderid))->find();
			if($order){
				$to_price=0;
				$toprice=0;
				
				M('bb_order_yifu')->where(array('orderid'=>$orderid))->delete();
				foreach ($array as $ks => $vs) {
					$yifu['orderid']=$vs['orderid'];
					$yifu['name']=$vs['name'];
					$yifu['price']=$vs['price']*0.01;
					$yifu['num']=$vs['num'];
					$yifu['to_price']=$vs['to_price']*0.01;
					$yifu['xiachi']=$vs['xiachi'];
					$yifu['fujian']=$vs['fujian'];
					M('bb_order_yifu')->add($yifu);
					$to_price=($to_price+$vs['to_price']);
				 	$toprice=($toprice+$vs['toprice']);
				}
				//======================
				if($p['imglist']){
					M('bb_order_img')->where(array('orderid'=>$orderid))->delete();
					foreach ($p['imglist'] as $k => $v) {
						$img['orderid']=$orderid;
						$img['imgurl']=$v;
						M('bb_order_img')->add($img);
					}
				}
				//==========================
				if($to_price==$toprice){
					$order['total_price']=$toprice*0.01;
					$re=M('bb_order')->save($order);
					if($re){
						$info['status']=1;
						$info['msg']='yes';
					}else{
						$info['status']=0;
						$info['msg']='no';
					}
				}else{
					$info['status']=0;
					$info['msg']='no';
				}
				$this->ajaxreturn($info);
			}
		}
	}
	//==================
	//阿姨抢单
	public function qiangdan(){
		if(IS_POST){
			$p=I('post.');
			$p['shopid']=self::$session['auntie_shop']['shopid'];
			//检查订单是否被别的阿姨抢了
			$orders=M('bb_order')->where($p)->find();
			if($orders['auntieid']){
				//订单已被人抢，判断是否为自己
				if($orders['auntieid']==self::$session['auntie_user']['id']){
					$info['status']=1;
					$info['msg']='抢单成功！';
				}else{
					$info['status']=0;
					$info['msg']='该订单已被别人抢了';
				}
			}else{
				//订单还没有被抢
				$p['shopid']=self::$session['auntie_shop']['shopid'];
				$p['shopname']=self::$session['auntie_shop']['name'];
				$p['shopuser']=self::$session['auntie_shop']['owner'];
				$p['shopphone']=self::$session['auntie_shop']['mobile'];
				$p['auntieid']=self::$session['auntie_user']['id'];
				$p['status']=1;
				
				$re=M('bb_order')->where(array('orderid'=>$p['orderid']))->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='抢单成功！';
					//=====================
					$order=M('bb_order')->where(array('orderid'=>$p['orderid']))->find();
					$s_u['user_openid']=$order['openid'];
					$s_u['shopid']=$order['shopid'];
					$us=M('bb_shop_user')->where($s_u)->find();
					if(!$us){
						M('bb_shop_user')->add($s_u);
					}
				}else{
					$info['status']=0;
					$info['msg']='抢单失败！';
				}
				
			}
			$this->ajaxreturn($info);
		}
	}
	//添加阿姨信息
	public function add_auntie(){
		if(IS_POST){
			$p=I('post.');
			if($p['id']){
				$re=M('bb_auntie')->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='无修改！';
				}
			}else{
				$p['openid']=self::$session['openid'];
				$res=M('bb_auntie')->where(array('openid'=>$p['openid']))->find();
				if(!$res){
					$p['addtime']=date("Y-m-d H:i:s",time());
					$re=M('bb_auntie')->add($p);
					if($re){
						$info['status']=1;
						$info['msg']='添加成功！';
					}else{
						$info['status']=0;
						$info['msg']='添加失败！';
					}
				}else{
					$info['status']=0;
					$info['msg']='存在！';
				}
			}
		    $this->ajaxreturn($info);
		}
	}
//==============
	//修改用户信息
	public function edit_user(){
		if(IS_POST){
			$p=I('post.');
			$p['edittime']=time();
			$re=M('bb_auntie')->save($p);
			if($re){
				$info['status']=1;
				$info['msg']='ok！';
				$auntie_user=M('bb_auntie')->where(array('id'=>$p['id']))->find();
				$_SESSION['auntie']['auntie_user']=$auntie_user;
			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);
		}
	}
	//=========
	//下载微信接口的图片
	public function dow_img(){
		if(IS_POST){
			$p=I('post.');
			$options['appid']=C('BB_APPID_auntie');
			$options['secret']=C('BB_SECRET_auntie');
			$wx=new \bb\Wechat($options);
			$path=$wx->download_medis($p['serverid']);	//下载图片到本地
			if($path){
				$info['status']=1;
				$info['msg']=$path;
			}
			$this->ajaxreturn($info);
		}
	}
}